package org.sundatagroup.aurora.core.entity.particle;

import org.sundatagroup.aurora.core.entity.model.ParticleModel;
import org.sundatagroup.aurora.core.entity.pojo.Project;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 * <p>限定是指定工程的默认原子配置
 * </p>
 * 
 * @author Zdream
 * @date 2018-06-08
 * @since 0.2.0
 */
public class ProjectParticle extends Particle {
	
	/**
	 * @see
	 *   Project#getPid()
	 */
	int project;

	@Override
	public ParticleRule[] rules() {
		return new ParticleRule[] {ParticleRule.PROJECT};
	}

	public int getProject() {
		return project;
	}

	@Override
	public String toString() {
		return "ProjectParticle [#" + getId() + ", data=" + data + ", desc=" + desc
				+ ", project=" + project + "]";
	}

	@Override
	public ParticleModel toModel() {
		ParticleModel pm = super.toModel();
		
		JsonObject jo = new JsonObject();
		jo.addProperty("project", project);
		pm.setRule(jo.toString());
		
		return pm;
	}
	
	/**
	 * 生成粒子类
	 * @param pm
	 * @return
	 */
	public static ProjectParticle of(ParticleModel pm) {
		ProjectParticle p = new ProjectParticle();
		Particle.of(p, pm);
		
		JsonParser parser = ParticleFactory.parser;
		JsonObject jo = parser.parse(pm.getRule()).getAsJsonObject();
		
		p.project = jo.get("project").getAsInt();
		
		return p;
	}

}
